package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*学生登录*/
@WebServlet("/loginStudentServlet")
public class loginStudentSevlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户名
        String username = req.getParameter("username");

        // 判断用户名是否为空
        if (username == null || "".equals(username)){
            // 用户名为空，重定向到登录页面
            resp.sendRedirect("/login.jsp");
            return;
        }
            // 用户名不为空，将用户名存入会话域
            req.getSession().setAttribute("username",username);
        // 保存了用户名后，重定向到index.jsp再次进行判断
        resp.sendRedirect("/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
